package com.easylinkin.linkappapi.application.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.application.entity.Application;
import com.easylinkin.linkappapi.application.entity.ApplicationRefDeviceUnitDTO;
import com.easylinkin.linkappapi.application.entity.ApplicationRefPrivilegeDTO;
import com.easylinkin.linkappapi.application.service.ApplicationService;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 应用管理表 前端控制器
 * </p>
 *
 * @author XuGuoQing
 * @since 2020-05-19
 */
@RestController
@Api(value="应用管理",tags={"应用管理"})
@RequestMapping("/application")
public class ApplicationController {

    @Resource
    private ApplicationService applicationService;

    @ApiOperation("新增应用")
    @CommonOperateLogAnnotate(module = LogModule.APPLICATION, desc = "新增应用")
    @PostMapping("/addApplication")
    public RestMessage addApplication(@RequestBody Application application) {
        return RestBuilders.successBuilder().success(applicationService.addApplication(application)).build();
    }

    @ApiOperation("编辑应用")
    @CommonOperateLogAnnotate(module = LogModule.APPLICATION, desc = "编辑应用")
    @PostMapping("/updateApplication")
    public RestMessage updateLinkappAsset(@RequestBody  Application application) {
        return RestBuilders.successBuilder().success(applicationService.updateApplication(application)).build();
    }

    @ApiOperation("删除应用")
    @CommonOperateLogAnnotate(module = LogModule.APPLICATION, desc = "删除应用")
    @PostMapping("/deleteApplication")
    public RestMessage deleteLinkappAsset(@RequestBody List<String>list) {
        return RestBuilders.successBuilder().success(applicationService.deleteApplication(list)).build();
    }

    @ApiOperation("应用列表分页")
    @PostMapping("/getPage")
    public RestMessage getPage(@RequestBody RequestModel<Application> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<Application> record = applicationService.getPage(requestModel.getPage(), requestModel.getCustomQueryParams());
        return RestBuilders.successBuilder().data(record).build();
    }

    @ApiOperation("获取全部应用列表")
    @PostMapping("/getList")
    public RestMessage getList() {
      List <Application>  applicationList= applicationService.getApplicationList();
        return RestBuilders.successBuilder().data(applicationList).build();
    }

    @ApiOperation("根据应用ID获取其关联的设备型号列表")
    @GetMapping("/selectDeviceUnitByApplication/{applicationId}")
    public RestMessage selectDeviceUnitByApplication(@PathVariable String applicationId) {
      return RestBuilders.successBuilder().data(applicationService.selectDeviceUnitByApplication(applicationId)).build();
    }

    @ApiOperation("")
	  @PostMapping("/application2DeviceUnits")
    public RestMessage application2DeviceUnits(@RequestBody ApplicationRefDeviceUnitDTO applicationRefDeviceUnitDTO) {
		applicationService.application2DeviceUnits(applicationRefDeviceUnitDTO);
        return RestBuilders.successMessage();
    }

    @ApiOperation("根据应用ID获取其关联的菜单权限列表")
    @GetMapping("/selectPrivilegeByApplication/{applicationId}")
    public RestMessage selectPrivilegeByApplication(@PathVariable String applicationId) {
      return RestBuilders.successBuilder().data(applicationService.selectPrivilegeByApplication(applicationId)).build();
    }

    /**
     * 和上面的selectPrivilegeByApplication/{applicationId}接口一样的，不知道干啥的，传参也没用
     * @param applicationRefPrivilegeDTO
     * @return
     */
    @ApiOperation("")
    @PostMapping("/selectPrivilegeByApplication")
    public RestMessage selectPrivilegeByApplication(@RequestBody ApplicationRefPrivilegeDTO applicationRefPrivilegeDTO) {
      return RestBuilders.successBuilder().data(applicationService.selectPrivilegeByApplication("")).build();
    }

    @ApiOperation("")
	  @PostMapping("/application2Privileges")
    public RestMessage application2Privileges(@RequestBody ApplicationRefPrivilegeDTO applicationRefPrivilegeDTO) {
		applicationService.application2Privileges(applicationRefPrivilegeDTO);
        return RestBuilders.successMessage();
    }

    @ApiOperation("根据用户ID获取关联的应用列表")
    @GetMapping("/selectApplicationByUser/{userId}")
    public RestMessage selectApplicationByUser(@PathVariable String userId) {
      return RestBuilders.successBuilder().data(applicationService.selectApplicationByUser(userId)).build();
    }

    /**
     * 和上面selectApplicationByUser/{userId}接口一样的
     * @param userId
     * @return
     */
    @ApiOperation("")
    @GetMapping("/selectApplicationPrivilegeByUser/{userId}")
    public RestMessage selectApplicationPrivilegeByUser(@PathVariable String userId) {
      return RestBuilders.successBuilder().data(applicationService.selectApplicationByUser(userId)).build();
    }

}
